
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums}  from '../../common/sysenum.js'

import LXCOM from '../../common/common.js'

class RectGraphAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
        this.firstPoint = new LXCOM.LGXPoint();
    }

    act2Mouse(mosEvnt, worldPos) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                this.firstPoint.clone(worldPos);
                position.clear();
                this.init4Point(worldPos.x, worldPos.y, position);
                graph.setStart(1);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                this.correctRectPos(position, worldPos);
                graph.setFinish(1);
                state = LGXEnums.LGXPlotState.FinistState;
                graph.mouseDownTime = 0;
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseUp
            && graph.m_graphType == LGXEnums.LGXGraphType.MultiSel_Rect_Type) {
            this.correctRectPos(position, worldPos);
            graph.setFinish(1);
            state = LGXEnums.LGXPlotState.FinistState;
            graph.mouseDownTime = 0;
        }
        else if ((mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag)
            && graph.getFinish() == 0) {
            this.correctRectPos(position, worldPos);
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }

    modifyByCMD(isNew, cmd) {
        let rst = null;
        var graph = this.getRefGraph();
        var position = graph.getPos();
        let coordLst = cmd.split(",");

        if (cmd == "e") {
            graph.setFinish(1);
            graph.mouseDownTime = 0;

            let cmd = { type: 'e', value: "" };
            let nextTips = "";
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
        }
        else if (coordLst.length == 2) {
            let xdesc = coordLst[0];
            let ydesc = coordLst[1];

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return this.tips2RectSecondPoint(cmd);
                }
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                this.firstPoint.x = x;
                this.firstPoint.y = y;

                position.clear();
                this.init4Point(x, y, position);

                graph.mouseDownTime = 1;
                graph.setStart(1);
                rst = this.tips2RectSecondPoint(cmd);
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                let relatePoint = this.firstPoint;
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                let XRelateIdx = xdesc.indexOf("@");
                if (XRelateIdx >= 0) {
                    let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                    x = relatePoint.x + parseFloat(tmp);
                    y = relatePoint.y + y;
                }

                let inputWidFlag = false;
                let inputHgtFlag = false;
                let widDesc = "";
                if (xdesc.indexOf("w") >= 0) {
                    inputWidFlag = true;
                    widDesc = xdesc;
                }
                else if (ydesc.indexOf("w") >= 0) {
                    inputWidFlag = true;
                    widDesc = ydesc;
                }

                let hgtDesc = "";
                if (xdesc.indexOf("h") >= 0) {
                    inputHgtFlag = true;
                    hgtDesc = xdesc;
                }
                else if (ydesc.indexOf("h") >= 0) {
                    inputHgtFlag = true;
                    hgtDesc = ydesc;
                }

                if ((inputWidFlag && !inputHgtFlag) || (!inputWidFlag && inputHgtFlag)) {
                    return this.tips2RectSecondPoint(cmd);
                }

                if (widDesc && hgtDesc && widDesc.length > 1 && hgtDesc.length > 1) {
                    let strWid = widDesc.substr(1, widDesc.length - 1);
                    let strHgt = hgtDesc.substr(1, hgtDesc.length - 1);
                    let w = parseFloat(strWid);
                    let h = parseFloat(strHgt);

                    let oppositePt = position.posList[2];
                    let xflag = (oppositePt.x - relatePoint.x) / Math.abs(oppositePt.x - relatePoint.x);
                    let yflag = (oppositePt.y - relatePoint.y) / Math.abs(oppositePt.y - relatePoint.y);
                    x = relatePoint.x + xflag * w;
                    y = relatePoint.y + yflag * h;
                }

                let curentPt = { x, y };
                this.correctRectPos(position, curentPt);

                graph.setFinish(1);
                graph.mouseDownTime = 0;

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmdObj = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "";
                let appendToPreCMD = "";
                rst = { cmdObj, nextTips, appendToPreCMD, finish: true };
            }
        }
        if (!rst){
            rst = this.tips2RectSecondPoint(cmd);
        }
        graph.calScope(1);
        return rst;
    }

    tips2RectSecondPoint(cmd) {
        let cmdObj = { type: 'input ', value: cmd };
        let nextTips = "输入(x,y)或在图中点击确定下一对角点坐标。相对坐标使用:@x,y。长宽使用：(w数字,h数字)，如（w20,h20）。使用当前位置（e）";
        let appendToPreCMD = "";
        let rst = { cmdObj, nextTips, appendToPreCMD, finish: false };
        return rst;
    }

    init4Point(x, y, position) {
        var p1 = new LXCOM.LGXPoint();
        p1.x = x;
        p1.y = y;

        var p2 = new LXCOM.LGXPoint();
        p2.x = x;
        p2.y = y;

        var p3 = new LXCOM.LGXPoint();
        p3.x = x;
        p3.y = y;

        var p4 = new LXCOM.LGXPoint();
        p4.x = x;
        p4.y = y;
        position.posList.push(p1);
        position.posList.push(p2);
        position.posList.push(p3);
        position.posList.push(p4);
    }

    correctRectPos(position, mousePos) {
        var p1 = position.posList[0];
        var p2 = position.posList[1];
        var p3 = position.posList[2];
        var p4 = position.posList[3];

        p3.x = mousePos.x;
        p3.y = mousePos.y;

        p2.y = mousePos.y;
        p4.x = mousePos.x;
    }

    setTestCoord(position) {
        var p1 = position.posList[0];
        p1.x = 100;
        p1.y = 200;
        var p2 = position.posList[1];
        p2.x = 300;
        p2.y = 200;
        var p3 = position.posList[2];
        p3.x = 300;
        p3.y = 100;
        var p4 = position.posList[3];
        p4.x = 100;
        p4.y = 100;
    }
}

export default { RectGraphAction }
